<?php

/**
 * SigmaCMS - Content Management System
 *
 * Copyright (C) 2008 Pavol Biely
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 *
 * @package    SigmaCMS
 * @author     Pavol Biely <pavolbiely@gmail.com>
 * @copyright  2008 Pavol Biely
 * @license    http://www.gnu.org/licenses/gpl.txt   GNU General Public License
 * @link       http://pabi3.com/
 * @since      0.1.0
 */

final class Admin_Configuration
{
	/**
	 * Initialization
	 *
	 * @return void
	 */
	public function initialize()
	{
		Admin::header(_ADMIN_CONFIGURATION);

		echo "<h1>"._ADMIN_CONFIGURATION."</h1>\n";

		if (isset($_POST['submit'])) {
			// general settings
			db::query("UPDATE ".db::prefix('configuration')." SET `value`='".db::escape(_post('h1'))."' WHERE `name`='h1'");
			db::query("UPDATE ".db::prefix('configuration')." SET `value`='".db::escape(_post('title'))."' WHERE `name`='title'");
			db::query("UPDATE ".db::prefix('configuration')." SET `value`='".db::escape(_post('description'))."' WHERE `name`='description'");
			db::query("UPDATE ".db::prefix('configuration')." SET `value`='".db::escape(_post('keywords'))."' WHERE `name`='keywords'");
			
			// skin
			db::query("UPDATE ".db::prefix('configuration')." SET `value`='".db::escape(_post('skin'))."' WHERE `name`='skin'");
			db::query("UPDATE ".db::prefix('configuration')." SET `value`='".db::escape(_post('skin-admin'))."' WHERE `name`='skin_admin'");

			// language
			db::query("UPDATE ".db::prefix('configuration')." SET `value`='".db::escape(_post('language'))."' WHERE `name`='language'");

			// cookies
			db::query("UPDATE ".db::prefix('configuration')." SET `value`='".db::escape(_post('cookie_nameprefix'))."' WHERE `name`='cookie_nameprefix'");
			db::query("UPDATE ".db::prefix('configuration')." SET `value`='".db::escape(_post('cookie_expiration'))."' WHERE `name`='cookie_expiration'");
			db::query("UPDATE ".db::prefix('configuration')." SET `value`='".db::escape(_post('cookie_path'))."' WHERE `name`='cookie_path'");
			db::query("UPDATE ".db::prefix('configuration')." SET `value`='".db::escape(_post('cookie_domain'))."' WHERE `name`='cookie_domain'");
			db::query("UPDATE ".db::prefix('configuration')." SET `value`='".intval(_post('cookie_secure'))."' WHERE `name`='cookie_secure'");
			db::query("UPDATE ".db::prefix('configuration')." SET `value`='".intval(_post('cookie_httponly'))."' WHERE `name`='cookie_httponly'");

			// save data
			Actionlog::add(_LOG_ADMIN_CONFIGURATION);

			// event after configuration update
			Admin::addEvent('configUpdate');

			Admin::redirect('index.php?action=configuration');
		}

		echo "<form action='index.php?action=configuration' method='post'>\n";

		// general settings
		echo "<fieldset>\n";
		echo "<h2>"._ADMIN_CONFIGURATION_GENERAL."</h2>\n";

		echo "<label for='h1'>"._ADMIN_CONFIGURATION_GENERAL_H1." <small>&lt;h1&gt;</small></label>\n";
		echo "<input id='h1' name='h1' type='text' value='".cfg::get('h1')."' />\n";

		echo "<label for='title'>"._ADMIN_CONFIGURATION_GENERAL_TITLE." <small>&lt;title&gt;</small></label>\n";
		echo "<input id='title' name='title' type='text' value='".cfg::get('title')."' />\n";

		echo "<label for='description'>"._ADMIN_CONFIGURATION_GENERAL_DESCRIPTION."</label>\n";
		echo "<input id='description' name='description' type='text' value='".cfg::get('description')."' />\n";

		echo "<label for='keywords'>"._ADMIN_CONFIGURATION_GENERAL_KEYWORDS."</label>\n";
		echo "<input id='keywords' name='keywords' type='text' value='".cfg::get('keywords')."' />\n";

		echo "</fieldset>\n";

		// skin
		echo "<fieldset>\n";
		echo "<h2>"._ADMIN_CONFIGURATION_SKIN."</h2>\n";

		echo "<label for='skin'>"._ADMIN_CONFIGURATION_SKIN_PAGE."</label>\n";
		echo "<select id='skin' name='skin'>\n";
		$path  = dirname(__FILE__).'/../../'.cfg::DIR_SKINS;
		$skins = scandir($path);
		$skins = array_slice($skins,2);
		foreach ($skins as $key => $value) {
		    if (is_dir($path)) {
		    	$value .= '/';
		        if ($value == cfg::get('skin')) {
		        	echo "<option value='$value' selected='selected'>$value</option>\n";
		        } else {
                    echo "<option value='$value'>$value</option>\n";
		        }
		    }
		}
		echo "</select>\n";

		echo "<label for='skin-admin'>"._ADMIN_CONFIGURATION_SKIN_ADMIN."</label>\n";
		echo "<select id='skin-admin' name='skin-admin'>\n";
		$path = dirname(__FILE__).'/../skin/';
		foreach (glob($path.'*.css') as $key => $value) {
		    if (is_dir($path)) {
		    	$value = basename($value);
		        if ($value == cfg::get('skin_admin')) {
		        	echo "<option value='$value' selected='selected'>$value</option>\n";
		        } else {
                    echo "<option value='$value'>$value</option>\n";
		        }
		    }
		}
		echo "</select>\n";

		echo "</fieldset>\n";

		// language
		echo "<fieldset>\n";
		echo "<h2>"._ADMIN_CONFIGURATION_LANGUAGE."</h2>\n";

		echo "<label for='language'>"._ADMIN_CONFIGURATION_LANGUAGE."</label>\n";
		echo "<select id='language' name='language'>\n";
		// languages names
		$langs = i18n::getLanguages();

		// print languages
		$sql = db::query("SELECT * FROM ".db::prefix('languages'));
		while ($row = db::fetchObject($sql)) {
		    if (cfg::get('language') == $row->lang) {
		    	echo "<option value='".$row->lang."' selected='selected'>".$langs[$row->lang]."</option>\n";
		    } else {
                echo "<option value='".$row->lang."'>".$langs[$row->lang]."</option>\n";
		    }
		}
		db::free($sql);

		echo "</select>\n";

		echo "</fieldset>\n";

		// cookies
		echo "<fieldset>\n";
		echo "<h2>"._ADMIN_CONFIGURATION_COOKIES."</h2>\n";

		echo "<label for='cookie_nameprefix'>"._ADMIN_CONFIGURATION_COOKIES_NAME_PREFIX."</label>\n";
		echo "<input id='cookie_nameprefix' name='cookie_nameprefix' type='text' value='".cfg::get('cookie_nameprefix')."' />\n";

		echo "<label for='cookie_expiration'>"._ADMIN_CONFIGURATION_COOKIES_EXPIRATION."</label>\n";
		echo "<input id='cookie_expiration' name='cookie_expiration' type='text' value='".cfg::get('cookie_expiration')."' />\n";

		echo "<label for='cookie_path'>"._ADMIN_CONFIGURATION_COOKIES_PATH."</label>\n";
		echo "<input id='cookie_path' name='cookie_path' type='text' value='".cfg::get('cookie_path')."' />\n";

		echo "<label for='cookie_domain'>"._ADMIN_CONFIGURATION_COOKIES_DOMAIN."</label>\n";
		echo "<input id='cookie_domain' name='cookie_domain' type='text' value='".cfg::get('cookie_domain')."' />\n";

		$checked = NULL;
		if (cfg::get('cookie_secure')) {
			$checked = "checked='checked'";
		}
		echo "<label for='cookie_secure'>"._ADMIN_CONFIGURATION_COOKIES_SECURE."</label>\n";
		echo "<input id='cookie_secure' name='cookie_secure' type='checkbox' onclick='if(this.checked) alert(\""._ADMIN_CONFIGURATION_COOKIES_SECURE_NOTE."\");' value='1' $checked />\n";

		$checked = NULL;
		if (cfg::get('cookie_httponly')) {
			$checked = "checked='checked'";
		}
		echo "<label for='cookie_httponly'>"._ADMIN_CONFIGURATION_COOKIES_HTTPONLY."</label>\n";
		echo "<input id='cookie_httponly' name='cookie_httponly' type='checkbox' onclick='if(!this.checked) alert(\""._ADMIN_CONFIGURATION_COOKIES_HTTPONLY_NOTE."\")' value='1' $checked />\n";

		echo "<p class='center'>\n";
		echo "<input name='submit' type='submit' value='"._ADMIN_EDIT."' />\n";
		echo "</p>\n";

		echo "</fieldset>\n";
		echo "</form>\n";

		Admin::footer();
	}
}
